//
//  HSAskDetailCell.m
//  HolyShit
//
//  Created by Robin on 2017/3/29.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSAskDetailCell.h"

@interface HSAskDetailCell ()

@property (nonatomic, strong) UIImageView *questionIcon;
@property (nonatomic, strong) UILabel *questionNameLb;
@property (nonatomic, strong) UILabel *questionDescLb;
@property (nonatomic, strong) UIView *midLine;

@property (nonatomic, strong) UIImageView *answerIcon;
@property (nonatomic, strong) UILabel *answerNameLb;
@property (nonatomic, strong) UILabel *answerDescLb;

@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *likeLb;
@property (nonatomic, strong) UIView *bottomLine;


@end

@implementation HSAskDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
//    [self my_setSelectedBg];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = WHITE_COLOR;
    self.width = ScreenWidth;
    [self addSubview:self.questionIcon];
    [self addSubview:self.questionNameLb];
    [self addSubview:self.questionDescLb];
    [self addSubview:self.midLine];
    [self addSubview:self.answerIcon];
    [self addSubview:self.answerNameLb];
    [self addSubview:self.answerDescLb];
    [self addSubview:self.timeLb];
    [self addSubview:self.likeLb];
    [self addSubview:self.bottomLine];
}

- (void)setModel:(HSAskDetailModel *)model
{
    _model = model;
    [self.questionIcon my_setImage:model.question.userHeadPicUrl];
    self.questionNameLb.text = model.question.userName;
    self.questionDescLb.text = model.question.content;
    [self.questionDescLb sizeToFit];
    self.questionDescLb.width = self.questionNameLb.width;
    
    self.midLine.top = self.questionDescLb.bottom + 15;
    
    [self.answerIcon my_setImage:model.answer.specialistHeadPicUrl];
    self.answerIcon.top = self.midLine.bottom + 15;
    
    self.answerNameLb.text = model.answer.specialistName;
    self.answerNameLb.centerY = self.answerIcon.centerY;
    
    self.answerDescLb.text = model.answer.content;
    [self.answerDescLb sizeToFit];
    self.answerDescLb.width = self.answerNameLb.width;
    self.answerDescLb.top = self.answerNameLb.bottom + 10;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.answer.cTime / 1000];
    NSString *time = [date stringWithFormat:@"yyyy-MM-dd"];
    self.timeLb.text = time;
    self.timeLb.top = self.answerDescLb.bottom + 20;
    
    self.likeLb.text = [NSString stringWithFormat:@"%lu 赞       ", (unsigned long)model.answer.supportCount];
    self.likeLb.centerY = self.timeLb.centerY;
    
    self.bottomLine.top = self.timeLb.bottom + 20;
    
    if (model.cellHeight < 1) {
        model.cellHeight = self.bottomLine.bottom;
    }
}

- (void)tapWithStr:(NSString *)str
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancle];
    UIAlertAction *copy = [UIAlertAction actionWithTitle:@"复制" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [UIPasteboard generalPasteboard].string = str;
        [UIWindow showSuccessMsg:@"复制成功"];
    }];
    [alert addAction:copy];
    UIAlertAction *warn = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [UIWindow showSuccessMsg:@"举报成功"];
    }];
    [alert addAction:warn];
    [[UIViewController my_topViewController] presentViewController:alert animated:YES completion:nil];
}

#pragma mark - getter

- (UIImageView *)questionIcon{
    if(!_questionIcon){
        _questionIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 25, 35, 35) imageType:MYImageTypeHead];
        _questionIcon.layer.cornerRadius = 17.5;
    }
    return _questionIcon;
}

- (UILabel *)questionNameLb{
    if(!_questionNameLb){
        _questionNameLb = [UILabel labelWithFrame:CGRectMake(self.questionIcon.right + 10, 35, ScreenWidth - 60, 14) font:MYFont(Font28) color:HEXCOLOR(HSColorGray)];
    }
    return _questionNameLb;
}

- (UILabel *)questionDescLb{
    if(!_questionDescLb){
        _questionDescLb = [UILabel labelWithFrame:CGRectMake(self.questionNameLb.left, self.questionNameLb.bottom + 10, self.questionNameLb.width, 0) font:MYFont(Font32) color:HEXCOLOR(HSColorGray)];
        _questionDescLb.numberOfLines = 0;
        _questionDescLb.userInteractionEnabled = YES;
        @weakify(self);
        [_questionDescLb bk_whenTapped:^{
            @strongify(self);
            [self tapWithStr:self.questionDescLb.text];
        }];
    }
    return _questionDescLb;
}

- (UIView *)midLine{
    if(!_midLine){
        _midLine = [[UIView alloc] initWithFrame:CGRectMake(self.questionNameLb.left, 0, self.questionNameLb.width, 0.5)];
        _midLine.backgroundColor = HEXCOLOR(HSColorSeparateLight);
    }
    return _midLine;
}

- (UIImageView *)answerIcon{
    if(!_answerIcon){
        _answerIcon = [[UIImageView alloc] initWithFrame:CGRectMake(self.questionIcon.left, 0, 35, 35) imageType:MYImageTypeHead];
        _answerIcon.layer.cornerRadius = 17.5;
    }
    return _answerIcon;
}

- (UILabel *)answerNameLb{
    if(!_answerNameLb){
        _answerNameLb = [UILabel labelWithFrame:CGRectMake(self.questionNameLb.left, 0, self.questionNameLb.width, 14) font:MYFont(Font28) color:HEXCOLOR(HSColorGray)];
    }
    return _answerNameLb;
}

- (UILabel *)answerDescLb{
    if(!_answerDescLb){
        _answerDescLb = [UILabel labelWithFrame:CGRectMake(self.questionNameLb.left, 0, self.questionNameLb.width, 0) font:MYFont(Font32) color:HEXCOLOR(HSColorBlack)];
        _answerDescLb.numberOfLines = 0;
        _answerDescLb.userInteractionEnabled = YES;
        @weakify(self);
        [_answerDescLb bk_whenTapped:^{
            @strongify(self);
            [self tapWithStr:self.answerDescLb.text];
        }];
    }
    return _answerDescLb;
}

- (UILabel *)timeLb{
    if(!_timeLb){
        _timeLb = [UILabel labelWithFrame:CGRectMake(self.answerDescLb.left, 0, self.answerNameLb.width / 2, 12) font:MYFont(Font24) color:HEXCOLOR(HSColorGray)];
    }
    return _timeLb;
}

- (UILabel *)likeLb{
    if(!_likeLb){
        _likeLb = [UILabel labelWithFrame:CGRectMake(self.answerNameLb.left + self.answerNameLb.width / 2, 0, self.answerNameLb.width / 2, 12) font:MYFont(Font24) color:HEXCOLOR(HSColorGray)];
        _likeLb.textAlignment = NSTextAlignmentRight;
    }
    return _likeLb;
}

- (UIView *)bottomLine{
    if(!_bottomLine){
        _bottomLine =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 10)];
        _bottomLine.backgroundColor = HEXCOLOR(HSColorBgLight);
    }
    return _bottomLine;
}

@end
